思路:有多个频道(类别),每个频道下有多个商品链接,每个商品都有详情页。先将频道链接中的多个商品链接爬下来放入数据库中,再从数据库中取出来每一个商品详情页链接,进行详情页中的信息爬取
首先是channel_extact.py,爬取不同频道的链接
from bs4 import BeautifulSoup
import requests
start_url = 'http://bj.58.com/sale.shtml'
url_host = 'http://bj.58.com'
def get_index_url(url): #提取导航栏的链接,不同频道有不同的页面列表
# url = start_url
wb_data = requests.get(url)
soup = BeautifulSoup(wb_data.text, 'lxml')
links = soup.select('ul.ym-submnu > li > b > a') #抓取所有导航栏的链接
# print(links)
for link in links:
page_url = url_host + link.get('href') #补全链接
print(page_url)
get_index_url(start_url)
#长字符串,去除了手机号
channel_list = '''
http://bj.58.com/shouji/
http://bj.58.com/tongxunyw/
http://bj.58.com/diannao/
http://bj.58.com/bijiben/
ht